import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);

/* Layout */
import Layout from "@/layout";

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: "/login",
    component: () => import("@/views/login/login.vue"),
    hiden: true,
  },
  {
    path: "/",
    component: Layout,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        name: "Dashboard",
        component: () => import("@/views/dashboard/index"),
        meta: { title: "工作台", icon: "dashboard" },
      },
    ],
  },
];

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  {
    path: "/admin",
    name: "AdminManage",
    component: Layout,
    alwaysShow: true,
    meta: {
      title: "平台管理",
      icon: "chat",
      roles: ["AdminChatSearch"],
    },
    children: [
      {
        path: "/admin/chat/search",
        name: "AdminChatSearch",
        component: () => import("@/views/admin/adminChatSearch"),
        meta: { title: "聊天搜索", icon: "user", roles: ["AdminChatSearch"] },
      },
      {
        path: "/admin/map/search",
        name: "AdminMapSearch",
        component: () => import("@/views/admin/adminMapSearch"),
        meta: { title: "地图搜索", icon: "user", roles: ["AdminMapSearch"] },
      }
    ]
  },
  {
    path: "/chat",
    name: "ChatManage",
    component: Layout,
    alwaysShow: true,
    meta: {
      title: "恋信管理",
      icon: "chat",
      roles: ["ChatManage"],
    },
    children: [
      {
        path: "/chat/user/page",
        name: "ChatUserPage",
        component: () => import("@/views/chat/user"),
        meta: { title: "用户管理", icon: "user", roles: ["ChatUserPage"] },
      },
    ],
  },
  {
    path: "/mall",
    name: "MallManage",
    component: Layout,
    alwaysShow: true,
    meta: {
      title: "商城管理",
      icon: "mall",
      roles: [
        "MallManage",
        "MallGoodsManage",
        "MallGoodsCatalogPage",
        "MallGoodsAuditPage",
      ],
    },
    children: [
      {
        path: "/goods",
        name: "MallGoodsManagePage",
        component: () => import("@/views/mall/goods/index"),
        meta: {
          title: "商品管理",
          icon: "goods",
          roles: ["MallGoodsManagePage"],
        },
      },
      {
        path: "/mall/goods/quick/query",
        name: "MallGoodsQuickQueryPage",
        hidden: true,
        component: () => import("@/views/mall/goods/quick/index"),
        meta: {
          title: "快捷查询",
          icon: "quick-query",
          roles: ["MallGoodsQuickQueryPage"],
        },
      },
      {
        path: "/mall/goods/catalog",
        name: "MallGoodsCatalogPage",
        hidden: true,
        component: () => import("@/views/mall/goods/catalog/index"),
        meta: {
          title: "商品目录",
          icon: "goods-catalog",
          roles: ["MallGoodsCatalogPage"],
        },
      },
      {
        path: "/mall/goods/audit",
        name: "MallGoodsAuditPage",
        hidden: true,
        component: () => import("@/views/mall/goods/audit/index"),
        meta: {
          title: "商品审核",
          icon: "goods-audit",
          roles: ["MallGoodsAuditPage"],
        },
      },
      {
        path: "/mall/activity",
        name: "MallActivityPage",
        component: () => import("@/views/mall/activity/index"),
        meta: {
          title: "活动管理",
          icon: "activity-info",
          roles: ["MallActivityPage"],
        },
      },
    ],
  },
  {
    path: "/merchant",
    name: "MerchantManage",
    component: Layout,
    alwaysShow: true,
    meta: {
      title: "商户管理",
      icon: "merchant",
      roles: ["MerchantManage"],
    },
    children: [
      {
        path: "/merchant/info",
        name: "MerchantInfoPage",
        component: () => import("@/views/merchant/info/index"),
        meta: {
          title: "商户信息",
          icon: "merchantInfo",
          roles: ["MerchantInfoPage"],
        },
      },
      {
        path: "/merchant/info/detail",
        name: "MerchantDetailPage",
        hidden: true,
        component: () => import("@/views/merchant/info/detail"),
        meta: { title: "商户信息-详情", roles: ["MerchantDetailPage"] },
      },
      {
        path: "/merchant/database/mx",
        name: "MerchantsDatabaseMxPage",
        component: () => import("@/views/merchant/database/index"),
        meta: {
          title: "商户资源",
          icon: "database",
          roles: ["MerchantsDatabaseMxPage"],
        },
      },
      {
        path: "/merchant/menu/page",
        name: "MerchantMenuPage",
        component: () => import("@/views/merchant/menu/index"),
        meta: { title: "菜单权限", icon: "menu", roles: ["MerchantMenuPage"] },
      },
      {
        path: "/merchant/role/page",
        name: "MerchantRolePage",
        component: () => import("@/views/merchant/role/index"),
        meta: { title: "角色配置", icon: "role", roles: ["MerchantRolePage"] },
      },
    ],
  },
  {
    path: "/app",
    name: "AppManage",
    component: Layout,
    alwaysShow: true,
    meta: {
      title: "应用管理",
      icon: "app",
      roles: ["AppManage"],
    },
    children: [
      {
        path: "/app/version/page",
        name: "VersionPage",
        component: () => import("@/views/app/version/index"),
        meta: { title: "版本管理", icon: "version", roles: ["VersionPage"] },
      },
    ],
  },
  {
    path: "/resource",
    name: "ReourceManage",
    component: Layout,
    alwaysShow: true,
    meta: {
      title: "资源管理",
      icon: "resource",
      roles: ["ReourceManage", "ResourcePhoneCodePage"],
    },
    children: [
      {
        path: "/resource/phone/code/page",
        name: "ResourcePhoneCodePage",
        component: () => import("@/views/resource/phone-code/index"),
        meta: {
          title: "手机验证码",
          icon: "phone-code",
          roles: ["ResourcePhoneCodePage"],
        },
      },
    ],
  },
  {
    path: "/linux",
    name: "LinuxManage",
    component: Layout,
    alwaysShow: true,
    meta: {
      title: "服务管理",
      icon: "linux",
      roles: ["LinuxManage", "LinuxFolderPage"],
    },
    children: [
      {
        path: "/linux/folder/page",
        name: "LinuxFolderPage",
        component: () => import("@/views/linux/folder/index"),
        meta: {
          title: "文件管理",
          icon: "file-manage",
          roles: ["LinuxFolderPage"],
        },
      },
      {
        path: "/linux/docker/page",
        name: "LinuxDockerPage",
        component: () => import("@/views/linux/docker/index"),
        meta: {
          title: "DOCKER",
          icon: "docker-manage",
          roles: ["LinuxDockerPage"],
        },
      },
      {
        path: "/linux/web/page",
        name: "LinuxWebPage",
        component: () => import("@/views/linux/web/index"),
        meta: {
          title: "WEB管理",
          icon: "web-manage",
          roles: ["LinuxWebPage"],
        },
      },
      {
        path: "/linux/cmd/page",
        name: "LinuCmdPage",
        component: () => import("@/views/linux/cmd/index"),
        meta: {
          title: "CMD管理",
          icon: "cmd-manage",
          roles: ["LinuCmdPage"],
        },
      },
    ],
  },
  {
    path: "/system",
    name: "SystemManage",
    component: Layout,
    alwaysShow: true,
    meta: {
      title: "系统管理",
      icon: "system",
      roles: ["SystemManage"],
    },
    children: [
      {
        path: "/system/params/page",
        name: "ParamsPage",
        component: () => import("@/views/system/params/index"),
        meta: { title: "系统配置", icon: "params", roles: ["ParamsPage"] },
      },
      {
        path: "/system/menu/page",
        name: "MenuPage",
        component: () => import("@/views/system/menu/index"),
        meta: { title: "菜单权限", icon: "menu", roles: ["MenuPage"] },
      },
      {
        path: "/system/role/page",
        name: "RolePage",
        component: () => import("@/views/system/role/index"),
        meta: { title: "角色配置", icon: "role", roles: ["RolePage"] },
      },
      {
        path: "/system/user/page",
        name: "UserPage",
        component: () => import("@/views/system/user/index"),
        meta: { title: "账号管理", icon: "user", roles: ["UserPage"] },
      },
    ],
  },
];

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    base: "/chat/admin-web/",
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes,
  });

const router = createRouter();

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;
